package com.example.administrator.capital.mine.second;


import android.app.Fragment;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.presenter.LoginPresenter;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.utils.ToastUtil;
import com.example.administrator.capital.widget.CountDownTimerUtils;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;

/**
 * A simple {@link Fragment} subclass.
 * 设置交易密码
 */
public class SetDealPasswordFragment extends BaseBackFragment {
    @BindView(R.id.registerInBtn)
    Button mRegisterInBtn;
    Unbinder unbinder;
    @BindView(R.id.eidt_password)
    EditText mEidtPassword;
    @BindView(R.id.btn_eye)
    CheckBox mBtnEye;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.edit_code)
    EditText editCode;
    private TextView mBack;
    private MinePresenter minePresenter;
    private LoginPresenter mLoginPresenter;

    public SetDealPasswordFragment() {
        // Required empty public constructor
    }

    public static SetDealPasswordFragment newInstance() {

        Bundle args = new Bundle();
        SetDealPasswordFragment fragment = new SetDealPasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_set_deal_password, container, false);
        unbinder = ButterKnife.bind(this, view);
        minePresenter = new MinePresenter(null);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLoginPresenter = new LoginPresenter(null);
        isPasswordShowListener();
        mBack = view.findViewById(R.id.app_title_back);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();

            }
        });
        mRegisterInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getData();
            }
        });
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isPhoneNumber()) {
                    getYZM(phone.getText().toString());
                }

            }
        });

    }
    private boolean isPhoneNumber() {
        if (TextUtils.isEmpty(phone.getText().toString().trim())) {
            ToastUtil.showShort("手机号不能为空");
            return false;
        } else if (CommonUtils.isMobileNO(phone.getText().toString().trim())) {
            return true;
        } else {
            ToastUtil.showShort("手机号不合法");
            return false;
        }
    }

    @Override
    protected void setData() {
        super.setData();
    }
    private void getData() {
        minePresenter.updatapayword(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }

            @Override
            public void onSuccess(Response<String> data) {
                _mActivity.onBackPressed();
                EventBusActivityScope.getDefault(getActivity()).post(new EventBase("交易密码"));
                ToastUtil.showShort("交易密码设置成功");
            }

            @Override
            public void onFail(Response<String> error) {

            }
        }, editCode.getText().toString(), mEidtPassword.getText().toString().trim(), phone.getText().toString());
    }

    /*显示密码*/
    private void isPasswordShowListener() {
        mBtnEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = mEidtPassword.getText().length();
                mEidtPassword.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                mEidtPassword.setSelection(passwordLength);
            }
        });

    }

    private void getYZM(String mobile) {
        mLoginPresenter.getYZM(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);

            }

            @Override
            public void onSuccess(Response<String> data) {
                CountDownTimerUtils utils = new CountDownTimerUtils(getCode, 60000, 1000);
                utils.start();
            }

            @Override
            public void onFail(Response<String> error) {

            }
        }, mobile);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
